package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.Exception.BusinessException;
import com.itheima.commons.Code;
import com.itheima.commons.PageResult;
import com.itheima.commons.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    @GetMapping("search/{page}/{size}")
    public Result<User> getUsers(@RequestBody User user ,@PathVariable("page") int page, @PathVariable("size") int size) {


        PageInfo<User> userList = userService.getUsers(user,page, size);
        //重新封装一个对象   参数为数据列表 总记录数  总页数
        PageResult pageResult = new PageResult(userList.getTotal(), userList.getPages(), userList.getList());
        return new Result(Code.OK, pageResult, "查询成功");
    }


    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping("/{page}/{size}")
    public Result<User> getAll(@PathVariable("page") int page, @PathVariable("size") int size) {
        if (page<= 0 || size <=0){
            throw new BusinessException(new Result(Code.ERROR, "网络出现故障"));
        }

        //异常测试
//        int i = 1 / 0;
//        if (true) throw new BusinessException(new Result(Code.ERROR, "建议您换电脑"));
        PageInfo<User> userList = userService.getAll(page, size);
        //重新封装一个对象   参数为数据列表 总记录数  总页数
        PageResult pageResult = new PageResult(userList.getTotal(), userList.getPages(), userList.getList());
        return new Result(Code.OK, pageResult, "查询成功");
    }

    /**
     * 根据id查询
     *
     * @param uuid
     * @return
     */
    @GetMapping("{id}")
    public Result<User> get(@PathVariable("id") int uuid) {
        User user = userService.get(uuid);
        return new Result<User>(user != null ? Code.OK : Code.ERROR, user, user != null ? "查询成功" : "查询失败");
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @PostMapping
    public Result<User> save(@RequestBody User user) {
        boolean b = userService.save(user);
        return new Result<User>(b ? Code.OK : Code.ERROR, b ? "新增成功" : "新增失败");
    }

    /**
     * 删除用户
     *
     * @param uuid
     * @return
     */
    @DeleteMapping("{id}")
    public Result<User> delete(@PathVariable("id") int uuid) {
        boolean b = userService.delete(uuid);
        return new Result<User>(b ? Code.OK : Code.ERROR, b ? "删除成功" : "删除失败");
    }

    /**
     * 根据id 修改用户信息
     *
     * @param user
     * @return
     */
    @PutMapping
    public Result<User> update(@RequestBody User user) {
        boolean b = userService.update(user);
        return new Result<User>(b ? Code.OK : Code.ERROR, b ? "修改成功" : "修改失败");
    }

    /**
     * 根据用户名和密码查询
     *
     * @param userName
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Result<User> login(String userName, String password) {
        User user = userService.login(userName, password);
        return new Result<User>(user != null ? Code.OK : Code.ERROR, user, user != null ? "查询成功" : "查询失败");
    }

    @DeleteMapping("many/{ids}")
    public Result<User> delete(@PathVariable("ids") String uuids) {
        boolean b = userService.deleteByUuids(uuids);
        return new Result<User>(b ? Code.OK : Code.ERROR, b ? "删除成功" : "删除失败");
    }
}
